package com.base.cn.platform.os.common.mybatis.sharding;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 查询处理
 *
 * @author s.li
 * @create 2019-05-23 11:08
 */
public class SelectSql {

    /**
     * 解析select的SQL
     * @param sqlSelect
     */
    public static void select(SQLSelect sqlSelect, List<SQLExprTableSource> tableNameList, Set<String> columnSet, Map<String,Object> shardingValues){
        SQLSelectQuery sqlSelectQuery = sqlSelect.getQuery();
        if(sqlSelectQuery instanceof MySqlSelectQueryBlock){
            MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock)sqlSelectQuery;
            SQLTableSource tableSource = queryBlock.getFrom();
            if(tableSource instanceof SQLExprTableSource){
                SQLExprTableSource exprTableSource = (SQLExprTableSource)tableSource;
                addTableName(exprTableSource,tableNameList);
            }else if(tableSource instanceof SQLSubqueryTableSource){
                SQLSubqueryTableSource subQueryTableSource = (SQLSubqueryTableSource) tableSource;
                subQueryTableSource.getSelect().getQuery();
                select(subQueryTableSource.getSelect(),tableNameList,columnSet,shardingValues);
            }else if(tableSource instanceof SQLJoinTableSource) {
                SQLJoinTableSource joinTableSource = (SQLJoinTableSource) tableSource;
                joinHandle(joinTableSource,tableNameList,columnSet,shardingValues);
            }
            SQLExpr sqlExpr = queryBlock.getWhere();
            if(sqlExpr !=null){
                WhereSql.whereHandle(sqlExpr,tableNameList,columnSet,shardingValues);
            }
        }
    }

    /**
     * 处理join查询
     * @param joinTableSource join的表数据
     * @param tableNameList 表名集合
     */
    private static void joinHandle(SQLJoinTableSource joinTableSource,List<SQLExprTableSource> tableNameList, Set<String> columnSet, Map<String,Object> shardingValues){
        JoinOnCondition.joinOn(joinTableSource.getCondition(),columnSet,shardingValues);
        SQLTableSource right = joinTableSource.getRight();
        if(right !=null){
            if(right instanceof SQLExprTableSource){
                addTableName((SQLExprTableSource)right,tableNameList);
            }
        }
        SQLTableSource left = joinTableSource.getLeft();
        if(left !=null){
            if(left instanceof SQLJoinTableSource){
                joinHandle((SQLJoinTableSource) left,tableNameList,columnSet,shardingValues);
            }else{
                if(left instanceof SQLExprTableSource){
                    addTableName((SQLExprTableSource)left,tableNameList);
                }
            }
        }
        List<SQLExpr> list = joinTableSource.getUsing();
        if(list !=null && !list.isEmpty()){

        }
    }

    /**
     * 添加表名
     * @param exprTableSource
     * @param tableNameList
     */
    private static void addTableName(SQLExprTableSource exprTableSource, List<SQLExprTableSource> tableNameList){
        if(tableNameList !=null){
            tableNameList.add(exprTableSource);
        }
    }
}
